//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo:{},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [],
    gonggao:"",
    fengcai:[],
    imgvideo:""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that=this;
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      }
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    setTimeout(function () {
      that.getannouncement();
      that.fcschool();
      that.getvideo();
      that.setData({
        userInfo: wx.getStorageSync("user")
      })
    }, 500)
    wx.request({
      url: `${app.globalData.apiurl}rotationChart/list`,
      method:"GET",
      data:{},
      success:res=>{
        console.log(res)
        let img=[]
        for(let i=0;i<res.data.length;i++){
          img.push(res.data[i].url)
        }
        console.log(img);
        this.setData({
            imgUrls:img
        })
      }
    })
  },
  //公告
  getannouncement:function(e){
    wx.request({
      url: `${app.globalData.apiurl}announcement/list`,
      method: "GET",
      data: {},
      success: res => {
        console.log(res)
        this.setData({
          gonggao: res.data
        })
        wx.setStorageSync("gonggao", res.data);
      }
    })
  },
  //视频专区
  getvideo: function (e) {
    wx.request({
      url: `${app.globalData.apiurl}video/list`,
      method: "GET",
      data: {},
      success: res => {
        console.log(res)
        this.setData({
          imgvideo: res.data
        })
        wx.setStorageSync("imgvideo", res.data);
      }
    })
  },
  //学校风采
  fcschool: function (e) {
    wx.request({
      url: `${app.globalData.apiurl}mien/list`,
      method: "GET",
      data: {},
      success: res => {
        console.log(res)
        this.setData({
          fengcai: res.data
        })
        wx.setStorageSync("fengcai", res.data);
      }
    })
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  gonggao:function(e){
    console.log(e)
    let id = e.target.id
    console.log(id)
    wx.navigateTo({
      url: `/pages/ggfc/ggfc?id=${id}`,
    }) 
  },
  fengcai:function(e){
    let id = e.target.id
    console.log(id)
    wx.navigateTo({
      url: `/pages/fcschool/fcschool?id=${id}`,
    })
  },
  zsb:function(e){
    wx.switchTab({
      url: '/pages/test/test',
    })
  },
  onShow: function () {
    this.setData({
      userInfo: wx.getStorageSync("user")
    })
  },

})
